﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace Sai_Se_Dung
{
    public class CommandHandler : ICommand
    {
        private Action<object> exeAction;
        private Func<object, bool> canExeFunc;

        public CommandHandler(Action<object> execAction)
        {
            this.exeAction = execAction;
        }

        public CommandHandler(Action<object> execAction, Func<object, bool> canExecFunc)
        {
            this.exeAction = execAction;
            this.canExeFunc = canExecFunc;
        }

        public bool CanExecute(object parameter)
        {
            if (canExeFunc != null)
                return canExeFunc.Invoke(parameter);
            else
                return true;
        }

        public event System.EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            if (exeAction != null)
                exeAction.Invoke(parameter);
        }

        public void OnCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                foreach (var v in CanExecuteChanged.GetInvocationList())
                {
                    try
                    {
                        v.DynamicInvoke(this, new EventArgs());
                    }
                    catch { }
                }
            }
        }

    }
}
